<?php

namespace App\Http\Service;

use App\Models\Admindeletelog;
use App\Models\Doubanpost;
use App\Http\Service\BaseService;
use App\Http\Service\ChatgptService;
use App\Http\Service\UserService;

class AdmindeletelogService  extends BaseService
{
    private $model;
    public  $userService;
    public function __construct(Admindeletelog $model,ChatgptService $chatgptService,UserService  $userService)
    {
        parent::__construct();
        $this->model = $model;
        $this->chatgptService=$chatgptService;
        $this->userService=$userService;
       
    }


    #豆瓣爆料违规通知列表
    public function getList($request,$data=[]){
        $page = !empty($data['page']) && intval($data['page']) ?$data['page'] :$this->cpage;
        //dd($page);
        $limit = !empty($data['limit']) && intval($data['limit']) ?$data['limit'] :$this->climit;
        $offset = ($page-1)*$limit;
        $type=$data['type'];
        $people_id=$data['people_id'];
       // dd($people_id);
        $where=[];
        $list=[];
        if($type=='1'){
            //逛逛
            $where[]=['A.type','=','2'];
            $where[]=['A.admin_id','=',$people_id];
            //$this->getLastSql();
            $count = $this->model->from('admin_delete_log as A') // 主表使用别名
            ->join('douban_post as B', 'A.post_id', '=', 'B.id') // 关联表使用别名
            ->where($where)->count();
            $fields = ['B.*','A.id as id','B.id as post_id','A.reason as reason','A.deleted_time as deleted_time'];
           // $this->getLastSql();
            $list = $this->model->select($fields)
            ->from('admin_delete_log as A') // 主表使用别名
            ->join('douban_post as B', 'A.post_id', '=', 'B.id') // 关联表使用别名
            ->where($where)
            ->orderBy('deleted_time', 'desc')
            ->offset($offset)->limit($limit)
            ->get()
            ->toArray();

        }else if($type=='2'){
            //爆料
            $where[]=['A.type','=','1'];
            $where[]=['A.admin_id','=',$people_id];
            $count = $this->model->from('admin_delete_log as A') // 主表使用别名
            ->join('baoliao_post as B', 'A.post_id', '=', 'B.id') // 关联表使用别名
            ->where($where)->count();
            $fields = ['B.*','A.id as id','B.id as post_id','A.reason as reason','A.deleted_time as deleted_time'];

            $list = $this->model->select($fields)
            ->from('admin_delete_log as A') // 主表使用别名
            ->join('baoliao_post as B', 'A.post_id', '=', 'B.id') // 关联表使用别名
            ->where($where)
            ->offset($offset)
            ->limit($limit)
            ->orderBy('deleted_time', 'desc')
            ->get()
            ->toArray();

        }else{
            //默认全部
            $where[]=['A.admin_id','=',$people_id];
            $count = $this->model->from('admin_delete_log as A') // 主表使用别名
            ->join('baoliao_post as B', 'A.post_id', '=', 'B.id') // 关联表使用别名
            ->where($where)->count();
            $fields = ['B.*','A.id as id','B.id as post_id','A.reason as reason','A.deleted_time as deleted_time'];

            $list = $this->model->select($fields)
            ->from('admin_delete_log as A') // 主表使用别名
            ->join('baoliao_post as B', 'A.post_id', '=', 'B.id') // 关联表使用别名
            ->where($where)
            ->offset($offset)
            ->limit($limit)
            ->orderBy('deleted_time', 'desc')
            ->get()
            ->toArray();

        }
        //dd($list);
        $result = [
            'page'=> $page,
            'limit'=> $limit,
            'total_page'=> ceil($count/$limit),
            'count'=>$count,
            'data'=> $list
        ];
        return $result; 
    }
   
    public function addDate($request,$data=[])
    {
        //获取所有json数据
        $jsonData = $request->json()->all();
        $data['json_data']=json_encode($jsonData, JSON_UNESCAPED_UNICODE);
        $ids=$data['ids'];
        foreach ($ids as $k => $v) {
            $hdata=[
                'type'=>$data['type'],
                'post_id'=>$v,
                'admin_id'=>$data['user_id'],
                'reason'=>$data['content'],
                'deleted_time'=>date("Y-m-d H:i:s"),
            ];
            //dd($hdata);
            
            $where = [
                ['admin_id', '=', $hdata['admin_id']],
                ['type', '=', $hdata['type']], 
                ['post_id', '=', $hdata['post_id']]
            ];
            //dd($where);
           /// $this->getLastSql();
           $first = $this->model->where($where)->first();
           if(!$first){
                $insertId=$this->model->insertGetId($hdata); 
           }

            
            
             
        }
      
        return true;
        
        
    }
    //删除
    public function del($request,$data=[],$ids)
    {
        $res =  $this->model->whereIn('id',$ids)->delete();
        if(!$res){
            $this->error('删除失败');
        }else{
            $this->success();
        }
    }   
}
